﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MachineBase : MonoBehaviour {

    private Vector3 gProcessPosition;//待加工放置区域
    private Vector3 gFinishPosition;//已完工放置区域
    private bool gIsWorking=false;//是否真正加工
    private bool gIsFinish=false;//是否已经结束
    private bool gIsNeedMaterial=true;//是否需要材料
    private float gProcessTime=10f;//零件加工时长
    private float gWorkedTime;//一个已加工时间
    private GameObject gProcessMaterial;//待加工材料
    private GameObject gFinishMaterial;//已完工材料

	// Use this for initialization
	void Start () {
        gWorkedTime = 0;//初始化已经加工时间0
    }
	
	// Update is called once per frame
	void Update () {
		if(gIsWorking)
        {
            gWorkedTime += Time.deltaTime;
            if (gWorkedTime >= gProcessTime)//加工完成
            {
                gWorkedTime = 0f;//参数重置
                gIsWorking = false;//停止工作
                gIsFinish = true;//已经完工
                FinishMaterial();//材料改变
            }
        }
	}

    /// <summary>
    /// 开始工作
    /// </summary>
    public void GetProcessMaterial(GameObject iProcessMaterial)
    {
        //进行注销
        if (iProcessMaterial != null)
            GameObject.Destroy(iProcessMaterial);

        gIsWorking = true;
        gIsFinish = false;
        gIsNeedMaterial = false;
        //获得材料并开始工作
        gProcessMaterial.SetActive(true);
        gFinishMaterial.SetActive(false);
    }

    /// <summary>
    /// 获取，机床是否正在加工
    /// </summary>
    public bool IsWork
    {
        get { return gIsWorking; }
    }

    public bool IsFinish
    {
        get { return gIsFinish; }
    }

    public bool IsNeedMaterial
    {
        get { return gIsNeedMaterial; }
    }

    public GameObject GetFinishMaterial()
    {
        //克隆一个材料给小车，原材料保留
        gProcessMaterial.SetActive(false);
        gFinishMaterial.SetActive(false);
        GameObject pFinishMaterial= GameObject.Instantiate(gFinishMaterial);
        gIsFinish = false;
        gIsNeedMaterial = true;
        return pFinishMaterial; 
    }

    /// <summary>
    /// 初始化Machie的相关设置
    /// 以后cong
    /// </summary>
    public void InitMachine(Vector3 iProcessPosition, Vector3 iFinishPosition, GameObject iPreProcessMaterial, GameObject iPreFinishMaterial, float iProcessTime)
    {
        //1、设置进出点位
        gProcessPosition = iProcessPosition;
        gFinishPosition = iFinishPosition;
        //2、设置材料
        gProcessMaterial = Instantiate(iPreProcessMaterial);
        gFinishMaterial = Instantiate(iPreFinishMaterial);
        //3、设置材料位置
        gProcessMaterial.transform.position = gProcessPosition;
        gFinishMaterial.transform.position = gFinishPosition;
        //4、设置加工时长
        gProcessTime = iProcessTime;
        //5、设置材料均为不可见
        gProcessMaterial.SetActive(false);
        gFinishMaterial.SetActive(false);
    }

    /// <summary>
    /// 加工完成
    /// </summary>
    private void FinishMaterial()
    {
        //1、待加工零件注销
        gProcessMaterial.SetActive(false);
        //2、零件改变
        gFinishMaterial.SetActive(true);
    }


}

////直接查找 并初始化
//GameObject login_layer = Instantiate(Resource.Load("Prefabs/login_layer").gameobject);
//GameObject canvas = GameObject.Find("Canvas");
//login_layer.transform.parent = canvas.transform;

